package com.jiuqi.abc.model.dao;

import com.jiuqi.abc.model.bean.DriverItem;
import com.jiuqi.bi.util.OrderGenerator;
import com.jiuqi.bi.util.type.GUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.*;

/**
 * 动因项DAO
 * <p>
 * 概念区分
 * 维度    name（英文）  title（中文）
 * 维度项  code（英文）  name（中文）
 */
@Repository
public class ABCDriverItemDao {

    //级联删除使用
    @Autowired
    ABCDriverItemFormulaDao abcDriverItemFormulaDao;

    //动因项表
    private static final String T_TABLE = "ABC_M_DRIVER_VALUE";

    //表ID(主键)
    private static final String F_AMDV_ID = "AMDV_ID";

    //所属动因ID 与 动因表 中 表ID(主键)关联
    private static final String F_AMDR_ID = "AMDR_ID";

    //动因项编码
    private static final String F_AMDV_CODE = "AMDV_CODE";

    //关联成本维度编码 dimName
    private static final String F_AMMD_DIM_CODE = "AMMD_DIM_CODE";

    //关联陈本维度项编码 dimItemCode
    private static final String F_AMMD_DIM_ITEM_CODE = "AMMD_DIM_ITEM_CODE";

    //动因项顺序
    private static final String F_AMDV_ORDER = "AMDV_ORDER";

    //所属动因分组ID 与 动因分组表 中 表ID(主键)关联
    private static final String F_AMDG_ID = "AMDG_ID";

    //所属模型ID 与 模型表 中 表ID(主键)关联
    private static final String F_AMM_ID = "AMM_ID";

    //动因项名称
    private static final String F_AMDV_NAME = "AMDV_NAME";

    @Autowired
    JdbcTemplate jdbcTemplate;


    //【增】 动因项
    public void batchAddDriverItem(List<DriverItem> driverItems) {
        String addSql = String.format("INSERT INTO %s VALUES(?,?,?,?,?,?,?,?,?)", T_TABLE);
        List<Object[]> argList = new ArrayList<>();
        for (DriverItem driverItem : driverItems) {

            if (Objects.isNull(driverItem.getId()) || ("").equals(driverItem.getId()))
                driverItem.setId(GUID.newGUID());

            Object[] args = {driverItem.getId(), driverItem.getBelongDriverId(), driverItem.getCode(),
                    driverItem.getDimName(), driverItem.getMemberCode(), OrderGenerator.newOrderID(),
                    driverItem.getBelongDriverGroupId(), driverItem.getBelongModelId(), driverItem.getName()};
            argList.add(args);
        }

        jdbcTemplate.batchUpdate(addSql, argList);
    }

    /**
     * 【查】 根据 动因ID集合 批量获取 动因项集合
     *
     * @param driverIdList 动因表主键集合
     * @return 动因项集合
     */
    public List<DriverItem> getDriverItemListByDriverIds(List<String> driverIdList) {
        //查询SQL语句
        String selectSql = String.format("SELECT %s,%s,%s,%s,%s,%s,%s,%s,%s FROM %s WHERE %s in ( :ids ) ORDER BY %s",
                F_AMDV_ID, F_AMDR_ID, F_AMDV_CODE, F_AMMD_DIM_CODE, F_AMMD_DIM_ITEM_CODE, F_AMDV_ORDER, F_AMDG_ID, F_AMM_ID, F_AMDV_NAME,
                T_TABLE,
                F_AMDR_ID,
                F_AMDV_ORDER);

        // 为空时添加""防止SQL语法错误
        if (driverIdList.isEmpty())
            driverIdList.add("");

        //配置查询参数
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("ids", driverIdList);

        //执行查询
        NamedParameterJdbcTemplate jdbc = new NamedParameterJdbcTemplate(jdbcTemplate);
        List<Map<String, Object>> resultList = jdbc.queryForList(selectSql, paramMap);

        //处理结果
        List<DriverItem> driverItemList = new ArrayList<>();
        for (Map<String, Object> stringObjectMap : resultList) {
            DriverItem driverItem = new DriverItem();
            driverItem.setId((String) stringObjectMap.get(F_AMDV_ID));
            driverItem.setBelongDriverId((String) stringObjectMap.get(F_AMDR_ID));
            driverItem.setCode((String) stringObjectMap.get(F_AMDV_CODE));
            driverItem.setDimName((String) stringObjectMap.get(F_AMMD_DIM_CODE));
            driverItem.setMemberCode((String) stringObjectMap.get(F_AMMD_DIM_ITEM_CODE));

            BigDecimal bigDecimalData = (BigDecimal) stringObjectMap.get(F_AMDV_ORDER);
            long longValue = bigDecimalData.longValue();
            driverItem.setOrder(longValue);

            driverItem.setBelongDriverGroupId((String) stringObjectMap.get(F_AMDG_ID));
            driverItem.setBelongModelId((String) stringObjectMap.get(F_AMM_ID));
            driverItem.setName((String) stringObjectMap.get(F_AMDV_NAME));

            driverItemList.add(driverItem);
        }

        //返回结果
        return driverItemList;
    }

    /**
     * 【查】 根据 动因分组ID 获取 动因项集合
     *
     * @param driverGroupId 动因分组ID
     * @return 动因项集合
     */
    public List<DriverItem> getDriverItemListByDriverGroupId(String driverGroupId) {
        //查询SQL语句
        String selectSql = String.format("SELECT %s,%s,%s,%s,%s,%s,%s,%s,%s FROM %s WHERE %s = ? ORDER BY %s",
                F_AMDV_ID, F_AMDR_ID, F_AMDV_CODE, F_AMMD_DIM_CODE, F_AMMD_DIM_ITEM_CODE, F_AMDV_ORDER, F_AMDG_ID, F_AMM_ID, F_AMDV_NAME,
                T_TABLE,
                F_AMDG_ID,
                F_AMDV_ORDER);

        //查询参数
        Object[] args = {driverGroupId};

        //执行查询、处理结果
        return jdbcTemplate.query(selectSql, rs -> {
            List<DriverItem> driverItemList = new ArrayList<>();
            while (rs.next()) {
                    DriverItem driverItem = new DriverItem();
                    driverItem.setId(rs.getString(F_AMDV_ID));
                    driverItem.setBelongDriverId(rs.getString(F_AMDR_ID));
                    driverItem.setCode(rs.getString(F_AMDV_CODE));
                    driverItem.setDimName(rs.getString(F_AMMD_DIM_CODE));
                    driverItem.setMemberCode(rs.getString(F_AMMD_DIM_ITEM_CODE));
                    driverItem.setOrder(rs.getLong(F_AMDV_ORDER));
                    driverItem.setBelongDriverGroupId(rs.getString(F_AMDG_ID));
                    driverItem.setBelongModelId(rs.getString(F_AMM_ID));
                    driverItem.setName(rs.getString(F_AMDV_NAME));

                    driverItemList.add(driverItem);
            }
            return driverItemList;
        }, args);
    }

    /**
     * 【查】 根据 模型ID 获取 动因项集合
     *
     * @param modelId 模型ID
     * @return 动因项集合
     */
    public List<DriverItem> getDriverItemListByModelId(String modelId) {
        //查询SQL语句
        String selectSql = String.format("SELECT %s,%s,%s,%s,%s,%s,%s,%s,%s FROM %s WHERE %s = ? ORDER BY %s",
                F_AMDV_ID, F_AMDR_ID, F_AMDV_CODE, F_AMMD_DIM_CODE, F_AMMD_DIM_ITEM_CODE, F_AMDV_ORDER, F_AMDG_ID, F_AMM_ID, F_AMDV_NAME,
                T_TABLE,
                F_AMM_ID,
                F_AMDV_ORDER);

        //查询参数
        Object[] args = {modelId};

        //执行查询、处理结果
        return jdbcTemplate.query(selectSql, rs -> {
            List<DriverItem> driverItemList = new ArrayList<>();
            while (rs.next()) {
                    DriverItem driverItem = new DriverItem();
                    driverItem.setId(rs.getString(F_AMDV_ID));
                    driverItem.setBelongDriverId(rs.getString(F_AMDR_ID));
                    driverItem.setCode(rs.getString(F_AMDV_CODE));
                    driverItem.setDimName(rs.getString(F_AMMD_DIM_CODE));
                    driverItem.setMemberCode(rs.getString(F_AMMD_DIM_ITEM_CODE));
                    driverItem.setOrder(rs.getLong(F_AMDV_ORDER));
                    driverItem.setBelongDriverGroupId(rs.getString(F_AMDG_ID));
                    driverItem.setBelongModelId(rs.getString(F_AMM_ID));
                    driverItem.setName(rs.getString(F_AMDV_NAME));

                    driverItemList.add(driverItem);
            }
            return driverItemList;
        }, args);
    }

    //【删】 根据 动因ID集合 批量删除 动因项
    public void batchDeleteDriverItem(List<String> driverIdList) {
        String deleteSql = String.format("DELETE FROM %s WHERE %s = ?", T_TABLE, F_AMDR_ID);
        List<Object[]> argsList = new ArrayList<>();
        for (String driverId : driverIdList) {
            argsList.add(new Object[]{driverId});
        }
        jdbcTemplate.batchUpdate(deleteSql, argsList);
    }
}
